package com.peanut.baby.mvp.follow;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.User;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.profile.ProfileActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/8/7.
 */

public class FollowFansActivity extends BaseMvpActivity implements TitleLayout.OnTitleClickListener,
    PullRecyclerView.OnRecyclerRefreshListener,
        FollowFansContract.View,
    FollowFansRecyclerAdapter.OnChangeFollowStatusListener{
    private static final String TAG = FollowFansActivity.class.getSimpleName();

    public boolean isFollow = false;
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecycler;

    private List<User>                  user;
    private FollowFansRecyclerAdapter   adapter;
    private FollowFansPresenter         presenter;

    private String userId = "";
    private int pageNo = 1;
    private int pageSize = 10;
    public static void start(Context ctx, boolean isFollow,String userId) {
        ctx.startActivity(new Intent(ctx, FollowFansActivity.class)
                .putExtra("isFollow", isFollow)
                .putExtra("userId",userId));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow_fans);
        if(InitManager.getInstance().getUser() == null
                || StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
            showToast("用户token错误");
            finish();
            return;
        }
        ButterKnife.bind(this);
        userId = getIntent().getStringExtra("userId");
        isFollow = getIntent().getBooleanExtra("isFollow",false);
        presenter = new FollowFansPresenter(this,this);
        initView();

    }

    private void initView(){
        title.setOnTitleClickedListener(this);
        title.setTitle(isFollow ? "关注":"粉丝");
        pullRecycler.setOnRecyclerRefreshListener(this);
        pullRecycler.setLayoutManager(new XLinearLayoutManager(this));
        user = new ArrayList<>();
        adapter = new FollowFansRecyclerAdapter(this,user);
        adapter.setOnFollowStatusChangeListener(this);

        pullRecycler.setAdapter(adapter);

        pullRecycler.postRefreshing();
    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        if(isFollow){
            presenter.getFollowList(userId,pageNo,pageSize);
        }else{
            presenter.getFansList(userId,pageNo,pageSize);
        }
    }

    @Override
    public void onLoadMore() {
        pageNo ++;
        if(isFollow){
            presenter.getFollowList(userId,pageNo,pageSize);
        }else{
            presenter.getFansList(userId,pageNo,pageSize);
        }
    }

    @Override
    public void onListGet(List<User> users) {
        stopRefreshAndLoading();
        if(pageNo == 1){
            user.clear();
        }
        if(users != null && !users.isEmpty()){
            user.addAll(users);
        }
        adapter.notifyDataSetChanged();
        pullRecycler.enableLoadMore(users != null && users.size() >= pageSize);
    }

    @Override
    public void onRequestFailed(String reason) {
        stopRefreshAndLoading();
        showToast(reason);
    }

    @Override
    public void onChangeFollowStatusSucess(User user) {
        dismissProgressDialog();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onChangeFollowStatusFailed(String reason) {
        dismissProgressDialog();
        showToast(reason);
        adapter.notifyDataSetChanged();
    }

    private void stopRefreshAndLoading(){
        pullRecycler.stopRefresh();
        pullRecycler.stopLoadMore();
    }

    @Override
    public void onChangeFollowStatus(User user) {
        if(InitManager.getInstance().getUser() == null){
            LoginActivity.startForResult(FollowFansActivity.this);
            adapter.notifyDataSetChanged();
            return;
        }
        if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
            BindMobileActivity.startForResult(FollowFansActivity.this);
            adapter.notifyDataSetChanged();
            return;
        }
        showProgressDialog("请稍候...",false);
        presenter.changeFollowStatus(user);
    }

    @Override
    public void onAvatarClickListener(User user) {
        ProfileActivity.start(FollowFansActivity.this,user.userId);
    }
}
